package kd.hc.cont;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IBillModel;
import kd.bos.entity.datamodel.events.LoadDataEventArgs;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.OpenStyle;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.i18n.mservice.I18nServiceHelper;
import kd.bos.i18n.mservice.utils.AmountConvertResult;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hc.utils.BusinessDataServiceHelperExt;
import kd.hc.utils.ShowParameterUtil;
import kd.occ.ocbase.common.util.Convert;
import kd.sdk.plugin.Plugin;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * 收入合同
 */
public class InContractBillPlugin extends AbstractBillPlugIn implements Plugin, BeforeF7SelectListener {

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {

    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        //收入合同变更单列表
        IBillModel billModel = (IBillModel) this.getModel();
        ShowParameterUtil.contractbillInContainerList("收入合同变更单", this.getView(), this.getModel(), "ec_inrevision", "hc95_ftbg_flex", billModel.getPKValue());
        ShowParameterUtil.contractbillInContainerList("收入合同签证单", this.getView(), this.getModel(), "ec_invisabill", "hc95_htqz_flex", billModel.getPKValue());
        ShowParameterUtil.contractbillInContainerList("合同结算", this.getView(), this.getModel(), "ec_in_contract_settle", "hc95_htjs_flex", billModel.getPKValue());
//        ShowParameterUtil.contractbillInContainerList("合同收款", this.getView(), this.getModel(), "ec_inrevision", "hc95_htsk_flex", billModel.getPKValue());
        ShowParameterUtil.contractbillInContainerList("合同决算", this.getView(), this.getModel(), "ec_in_finalsettle", "hc95_ftjs_flex", billModel.getPKValue());
//        ShowParameterUtil.contractbillInContainerList("开票申请", this.getView(), this.getModel(), "ec_inrevision", "hc95_kpsq_flex", billModel.getPKValue());
//        ShowParameterUtil.contractbillInContainerList("合同发票", this.getView(), this.getModel(), "ec_inrevision", "hc95_ftfb_flex", billModel.getPKValue());
//        ShowParameterUtil.contractbillInContainerList("支付条款", this.getView(), this.getModel(), "ec_inrevision", "hc95_zftk_flex", billModel.getPKValue());


    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        int rowIndex = e.getChangeSet()[0].getRowIndex();
        Object oldValue = e.getChangeSet()[0].getOldValue();
        Object newValue = e.getChangeSet()[0].getNewValue();
        if ("originaloftaxamount".equalsIgnoreCase(name)) {
            if (newValue != null) {
                String originaloftaxamountStr = Convert.toBigDecimal(newValue).toPlainString();
                AmountConvertResult amountConvertResult = I18nServiceHelper.amountConvertUppercase("ZH", "CNY", originaloftaxamountStr, "false");
                if (amountConvertResult.isSuccess()) {
                    String result = amountConvertResult.getResult();
                    this.getModel().setValue("hc95_dxhtjshj", result);
                }
            } else {
                this.getModel().setValue("hc95_dxhtjshj", null);
            }
        } else if ("hc95_yhsm".equalsIgnoreCase(name)) {
            if (newValue != null) {
                DynamicObject yhsm = BusinessDataServiceHelperExt.loadAssistantDataByObject(newValue);
                this.getModel().setValue("hc95_yhsl", yhsm.getBigDecimal("fdescription"));
            } else {
                this.getModel().setValue("hc95_yhsl", null);
            }
        }
    }

    @Override
    public void loadData(LoadDataEventArgs e) {
        super.loadData(e);
    }
}